package regex;

import java.util.*;

/**
 * @fileName: YuqingtongRegex
 * @author: Han
 * @date: 2018/5/29 14:43
 * @dscription: 解析文本内容
 */
public class YuqingtongRegex {

    /**
     * 解析页面内容
     *
     * @param pageContent 页面内容
     * @return 解析后放入容器
     */
    public static List<Map<String, String>> parseContent(String pageContent) {

        List<Map<String, String>> weiboList = new ArrayList<>();

        //切割源码
        List<String> blocks = Regex.splitByRegex(pageContent, "#form_content");
        //去掉第0个无效项
        blocks.remove(0);
        //解析
        for (String block : blocks) {
            try {
                Map<String, String> map = parseContentBlock(block);
                weiboList.add(map);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return weiboList;
    }

    /**
     * 解析每个记录块
     *
     * @param block 块
     * @return 解析内容
     */
    private static Map<String, String> parseContentBlock(String block) {

        Map<String, String> map = new LinkedHashMap<>(5);

        String yuqingtongDetailUrl = parseYuqingtongDetail(block);
        String title = parseTitle(block);
        String digest = parseDigest(block);
        String originalUrl = parseOriginalUrl(block);
        String sourceSite = parseSourceSite(block);
        String time = parseTime(block);

        String titleKey = "标题";
        String digestKey = "摘要";
        if ("新浪微博".equals(sourceSite)) {
            titleKey = "作者";
            digestKey = "内容";
        }
        map.put("时间", time);
        map.put(titleKey, title);
        map.put(digestKey, digest);
        map.put("原文链接", originalUrl);
        map.put("舆情通全文地址ID", yuqingtongDetailUrl);
        map.put("来源", sourceSite);

        return map;
    }

    /**
     * 解析舆情通中该记录的全文地址
     *
     * @param block 记录块
     * @return url
     */
    private static String parseYuqingtongDetail(String block) {

        return Regex.matchString(block, "(?<=goView\\('原文',')[\\d]*")
                .replaceAll("<[\\S\\s]*?[^<>]>", "")
                .replaceAll("[\\s]*", "");
    }

    /**
     * 解析标题(名称)
     *
     * @param block 单条记录
     * @return 标题(名称)
     */
    private static String parseTitle(String block) {

        return Regex.matchString(block, "(?<=goView\\('原文','[\\s\\S]{1,100}?[^>]>)[\\s\\S]*?[^</a>](?=</a>)")
                .replaceAll("<[\\S\\s]*?[^<>]>", "")
                .replaceAll("[\\s]*", "");
    }

    /**
     * 解析摘要(内容)
     *
     * @param block 单条记录
     * @return 摘要(内容)
     */
    private static String parseDigest(String block) {

        //原博内容
        String originalMsg = Regex.matchString(block, "(?<=<p >)[\\s\\S]*?[^</p>](?=</p>)");

        //转发内容
        String transmitMsg = Regex.matchString(block,"(?<=<p  class=\"zf\">)[\\s\\S]*?[^</p>](?=</p>)");

        String msg;
        if (!"null".equals(transmitMsg)){
            msg = originalMsg + "//" + transmitMsg;
        }else {
            msg = originalMsg;
        }
        return msg.replaceAll("<[\\S\\s]*?[^<>]>", "")
                .replaceAll("[\\s]*", "");
    }

    /**
     * 解析原文链接
     *
     * @param block 单条记录
     * @return 原文链接
     */
    private static String parseOriginalUrl(String block) {

        return Regex.matchString(block, "(?<=onclick=\"originalUrl\\(')[\\s\\S]*?[^'](?=')")
                .replaceAll("<[\\S\\s]*?[^<>]>", "")
                .replaceAll("[\\s]*", "");
    }

    /**
     * 解析源站点名称
     *
     * @param block 单条记录
     * @return 源站点名称
     */
    private static String parseSourceSite(String block) {

        return Regex.matchString(block, "(?<=goCaptureWebsite\\(')[\\s\\S]*?[^'](?=')")
                .replaceAll("<[\\S\\s]*?[^<>]>", "")
                .replaceAll("[\\s]*", "");
    }

    /**
     * 解析时间
     *
     * @param block 单条记录
     * @return 时间, 格式:yyyy-MM-dd HH:mm
     */
    private static String parseTime(String block) {

        String date = Regex.matchString(block, "(?<=prepend\\(')[\\d]{4}年[\\d]{1,2}月[\\d]{1,2}日");
        String time = Regex.matchString(block, "(?<=prepend\\(')[\\d]{1,2}:[\\d]{1,2}");
        return date + " " + time;
        //解析出时间块
        /*int start = block.indexOf("nowTime.format('yyyy')");
        int end = block.indexOf("</script>");
        block = block.substring(start, end);

        String[] lines = block.split("\n");
        start = lines[4].indexOf("prepend('") + 9;
        end = lines[4].indexOf("')");
        String time = lines[4].substring(start, end);
        start = lines[5].indexOf("prepend('") + 9;
        end = lines[5].indexOf("<br");

        return lines[5].substring(start, end) + " " + time;*/
    }

    /**
     * 解析舆情通抓取的页面内容
     *
     * @param pageContent 页面源码
     * @return 解析后的内容
     */
    public static String parseOriginalContent(String pageContent) {

        String originalMsg = Regex.matchString(pageContent, "(?<=weibo-content  contenttext[\\s\\S]{1,100}?[^>]>)" +
                "[\\s\\S]*?[^<div](?=<div)");
        //如果此条是转发的微博
        String transmitName = Regex.matchString(pageContent, "(?<=<h5 class=\"weibo-name mb10\">)[\\s\\S]*?[^</h5>]" +
                "(?=</h5>)");
        String transmitMsg = Regex.matchString(pageContent, "(?<=<p class=\"mb30 mt30\" >)[\\s\\S]*?[^</p>](?=</p>)");

        String msg;
        if (!"null".equals(transmitMsg)) {
            msg = originalMsg + "//@" + transmitName + ":" + transmitMsg;
        } else {
            msg = originalMsg;
        }
        msg = msg.replaceAll("<[\\S\\s]*?[^<>]>", "").replaceAll("[\\s]*", "");
        return msg;
    }

    /**
     * 匹配总页数
     *
     * @param pageContent 页面源码
     * @return 总页数
     */
    public static int parseTotalPages(String pageContent) {

        int totalPages = 0;
        try {
            String totalPageStr = Regex.matchString(pageContent, "(?<=twbsPagination\\(\\{totalPages: )[\\d]*(?=,)");
            totalPages = Integer.parseInt(totalPageStr);
        } catch (Exception ignore) {
        }

        return totalPages;
    }
}